package com.health.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.health.dao.PermissionDao;
import com.health.dao.RoleDao;
import com.health.dao.UserDao;
import com.health.service.UserService;
import com.healthcommon.pojo.Permission;
import com.healthcommon.pojo.Role;
import com.healthcommon.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Transactional
@Service(interfaceClass = UserService.class)
public class UserServiceImp implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    public User findUserByUsername(String username) {
        User user = userDao.findByname(username);

        if (user == null){
            return null;
        }

        Set<Role> roles = roleDao.findbyuserRole(user.getId());

        if (roles !=null && roles.size()>0){
            for (Role r:roles) {
                Set<Permission> permissions = permissionDao.findbyPermission(r.getId());
                if (permissions !=null && permissions.size()>0){
                    r.setPermissions(permissions);
                }
            }

            user.setRoles(roles);
        }

        return user;
    }

}
